package cn.wolfcode.cms.service.impl;

import cn.wolfcode.cms.domain.Employee;
import cn.wolfcode.cms.domain.Product;
import cn.wolfcode.cms.mapper.ProductMapper;
import cn.wolfcode.cms.query.ProductQueryObject;
import cn.wolfcode.cms.service.IProductService;
import cn.wolfcode.cms.util.ExcelUtil;
import cn.wolfcode.cms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    public void save(Product entity) {
        productMapper.insert(entity);
    }

    public void update(Product entity) {
        productMapper.updateByPrimaryKey(entity);
        //维护与角色的关系
    }

    public void delete(Long id) {

        productMapper.deleteByPrimaryKey(id);
    }

    public Product get(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    public List<Product> list() {
        return productMapper.selectAll();
    }

    @Override
    public PageResult query(ProductQueryObject qo) {
        //查总数
        int total = productMapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        //查数据
        List<Employee> rows = productMapper.queryForList(qo);
        return new PageResult(total, rows);
    }

    //总共有多少商品
    @Override
    public Object getTotalNumber() {
        return productMapper.getTotalNumber();
    }

    @Override
    public Object totalAmount() {
        return productMapper.totalAmount();
    }

    public Object getProductByPsName(String name) {
        return productMapper.getProductByPsName(name);
    }


    @Override
    public Object getIntegral() {
        return productMapper.getIntegral();
    }


    public void export(HttpServletResponse response) {
        //导出操作
        ExcelUtil.export(response);
    }

    @Override
    public Object getFirstLevel() {
        return productMapper.getFirstLevel();
    }

}
